<?php
/**
 * Created by PhpStorm.
 * User: WangQingxia
 * Date: 2019/2/21
 * Time: 16:01
 */
//引入文件
require_once 'f:/localproject/test/word/phpwordDir/PHPWord.php';
require_once 'f:/localproject/test/word/phpwordDir/PHPWord/IOFactory.php';
class PHPPlugin{

    //根据订单和模板下载合同
    public function down_compact(){
        //初始化PHPWord类
        $PHPWord = new PHPWord();
        //选择合同模板
        $docxinfo = 'compact1.docx';
        $document = $PHPWord->loadTemplate($docxinfo);
        //渲染数据
        $document->setValue('Value1','1201812225293224294');             //合同编号
        $document->setValue('Value2', '代理废旧塑料');                    //甲方
        $document->setValue('Value3', '重庆市重庆区奥斯卡的哈利实打实');   //收件地址
        $document->setValue('Value4', '张三丰');                          //联络人
        $document->setValue('Value5', '15633336666');                     //手机
        $document->setValue('Value6', '123@163.com');                     //电子邮箱
        $document->setValue('Value7', '李四');                            //客户经理姓名
        $document->setValue('Value8', '10022223333');                     //客户经理电话
        $document->setValue('Value9', '订单信息担保交易');                 //订单信息

        $md5         = md5(time().mt_rand(1000, 9999));
        $tmpFileName = substr($md5, 0, 8).'-'.substr($md5, 10, 4).'-'.mt_rand(1000, 9999).'-'.substr($md5, 20, 12);
        $filePath    = 'f:/localproject/test/upload/word/'.$tmpFileName;
        $file        = $filePath.'.docx';

        //保存临时文件
        $document->save($file);

        //下载pdf文件
        $this->wordtopdf($file,$tmpFileName);

        //下载word文件
        $this->down_word($file);
    }

    //下载Word文件
    public function down_word($file){
        $file_size = filesize($file);
        $downFileName = '合同书.docx';

        header("Cache-Control: public");
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length:".$file_size);
        header("Content-Disposition: attachment; filename={$downFileName}");
        readfile($file);
    }

    //使用缓冲区下载word文件
    public function ob_down_word($file){
        ob_start(); //打开缓冲区
        $fp = fopen($file,"r");
        $file_size = filesize($file);
        $downFileName = '合同书.docx';

        header("Cache-Control: public");
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length:".$file_size);
        header("Content-Disposition: attachment; filename={$downFileName}");
        header("Pragma:no-cache");
        header("Expires:0");
        $buffer = 1024;
        $file_count = 0;
        //向浏览输出回数据
        while(!feof($fp) && $file_count < $file_size){
            $file_con = fread($fp,$buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        ob_end_flush();//输出全部内容到浏览器
    }

    //word转pdf
    public function wordtopdf($file, $tmpFileName){
        $fileroot = 'f:/localproject/test/upload/pdf';

        $pdfPath     = $fileroot;//pdf文件保存的目录
        $pdfHomePath = $fileroot;//项目目录或项目中pdf保存的目录
        $doc_path    = $file;    //word文件地址
        if(!is_dir($pdfPath)){
            mkdir($pdfPath,0777,true);
        }
        //生成pdf
        shell_exec("export HOME={$pdfHomePath} && libreoffice --headless --invisible --convert-to pdf:writer_pdf_Export {$doc_path} --outdir {$pdfPath}");
        //下载pdf
        $pdfName  = $tmpFileName.".pdf";
        $pdfFile  = $pdfHomePath.$pdfName;
        $filesize = filesize($pdfFile);
        header("Content-Type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Accept-Length: ".$filesize);
        header("Content-Disposition: attachment; filename=".$pdfName);
        // PDF 源在 original.pdf 中
        readfile($pdfFile);
        die();
    }

}